package cn.orangelite.ai.controller;

import cn.hutool.http.Header;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import cn.orangelite.ai.domain.AiConfig;
import cn.orangelite.ai.domain.AiExternal;
import cn.orangelite.ai.domain.AiInitConfigData;
import cn.orangelite.ai.service.IAiConfigService;
import cn.orangelite.ai.service.IAiInitConfigDataService;
import cn.orangelite.common.core.utils.poi.ExcelUtil;
import cn.orangelite.common.core.web.controller.BaseController;
import cn.orangelite.common.core.web.domain.AjaxResult;
import cn.orangelite.common.core.web.page.TableDataInfo;
import cn.orangelite.common.log.annotation.Log;
import cn.orangelite.common.log.enums.BusinessType;
import cn.orangelite.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

/**
 * ai厂商信息配置Controller
 *
 * @author dongfaqing
 * @date 2024-01-10
 */
@RestController
@RequestMapping("/config")
public class AiConfigController extends BaseController
{
    @Autowired
    private IAiConfigService aiConfigService;

    @Autowired
    private IAiInitConfigDataService aiInitConfigDataService;

    /**
     * 查询ai厂商信息配置列表
     */
    @RequiresPermissions("ai:config:list")
    @GetMapping("/list")
    public TableDataInfo list(AiConfig aiConfig) throws IOException {
        startPage();
        List<AiConfig> list = aiConfigService.selectAiConfigList(aiConfig);
        return getDataTable(list);
    }

    /**
     * 导出ai厂商信息配置列表
     */
    @RequiresPermissions("ai:config:export")
    @Log(title = "ai厂商信息配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AiConfig aiConfig)
    {
        List<AiConfig> list = aiConfigService.selectAiConfigList(aiConfig);
        ExcelUtil<AiConfig> util = new ExcelUtil<AiConfig>(AiConfig.class);
        util.exportExcel(response, list, "ai厂商信息配置数据");
    }

    /**
     * 获取ai厂商信息配置详细信息
     */
    @RequiresPermissions("ai:config:query")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable("configId") Long configId)
    {
        return success(aiConfigService.selectAiConfigByConfigId(configId));
    }

    /**
     * 新增ai厂商信息配置
     */
    @RequiresPermissions("ai:config:add")
    @Log(title = "ai厂商信息配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AiConfig aiConfig)
    {
        return toAjax(aiConfigService.insertAiConfig(aiConfig));
    }

    /**
     * 修改ai厂商信息配置
     */
    @RequiresPermissions("ai:config:edit")
    @Log(title = "ai厂商信息配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AiConfig aiConfig)
    {
        return toAjax(aiConfigService.updateAiConfig(aiConfig));
    }

    /**
     * 删除ai厂商信息配置
     */
    @RequiresPermissions("ai:config:remove")
    @Log(title = "ai厂商信息配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds)
    {
        return toAjax(aiConfigService.deleteAiConfigByConfigIds(configIds));
    }

    @RequiresPermissions("ai:config:getInitConfigData")
    @GetMapping("/getInitConfigData")
    public AjaxResult getInitConfigData(){
        List<AiInitConfigData> list = aiInitConfigDataService.list();
        if(list.size()>0){
            return success(list.get(0));
        }else{
            return success(null);
        }
    }

    @RequiresPermissions("ai:config:setInitConfigData")
    @PostMapping("/setInitConfigData")
    public AjaxResult setInitConfigData(@RequestBody AiInitConfigData aiInitConfigData){
        List<AiInitConfigData> list = aiInitConfigDataService.list();
        if(list.size()>0){
            aiInitConfigData.setId(list.get(0).getId());
            aiInitConfigDataService.updateById(aiInitConfigData);
        }else{
            aiInitConfigDataService.save(aiInitConfigData);
        }
        return success();
    }


    /**
     * 加入我们,注册
     * @param aiExternal
     * @return
     */
    @PostMapping("/joinUs")
    public AjaxResult joinUs(@RequestBody AiExternal aiExternal) throws UnknownHostException {
        // 获取当前服务器的 InetAddress 对象
        InetAddress localHost = InetAddress.getLocalHost();
        // 获取 IP 地址
        String ip = localHost.getHostAddress();
        aiExternal.setIp(ip);
        String json = JSONUtil.toJsonStr(aiExternal);
        String response = HttpUtil.createPost("http://weai.71cms.net/prod-api/ai/external/joinUs")
                .header(Header.CONTENT_TYPE, "application/json")
                .body(json)
                .execute()
                .body();
        Integer code = Integer.valueOf(JSONUtil.parseObj(response).getStr("code"));
        String msg = JSONUtil.parseObj(response).getStr("msg");
        if(code==200){
            return success(msg);
        }else{
            return error(msg);
        }
    }

    /**
     * 检查是否加入我们
     * @return
     */
    @GetMapping("/checkJoinUs")
    public AjaxResult checkJoinUs() throws UnknownHostException {
        // 获取当前服务器的 InetAddress 对象
        InetAddress localHost = InetAddress.getLocalHost();
        // 获取 IP 地址
        String ip = localHost.getHostAddress();

        String response = HttpUtil.get("http://weai.71cms.net/prod-api/ai/external/checkJoinUs?ip="+ip);
        response = JSONUtil.parseObj(response).getStr("code");
        return success(response);
    }


}
